package action.weixin;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import common.wx.WxAuth;
import common.wx.WxAuth.WxUser;
import net.sf.json.JSONObject;
import security.MySecurityContextHandle;
import staticclass.CommonConfig;

public class WeixinAuthAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	public static Map<String, String> stateMap = new HashMap<String, String>();
	private String code;
	private String param;
	private String url;
	private String state;

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getParam() {
		return param;
	}

	public void setParam(String param) {
		this.param = param;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}
	
	public String getState() {
		return state;
	}
	
	public void setState(String state) {
		this.state = state;
	}

	public String wxopenid() {
		if (!StringUtils.isEmpty(code)) {
			WxUser wxUser = WxAuth.getByCode(code);
			if (null != wxUser) {
				url = getRedirectUrl();
				MySecurityContextHandle.setItem("wchatId", wxUser.getOpenId());
				return "success";
			}
		}
		setPnParam();
		String redirectUri = CommonConfig.domainName + CommonConfig.contextPath + "/weixinauth/wxopenid.action";
		url = WxAuth.getAuthUrl(WxAuth.AUTH_TYPE_BASE, state, redirectUri);
		return "success";
	}

	public String wxuserinfo() {
		if (!StringUtils.isEmpty(code)) {
			WxUser wxUser = WxAuth.getByCode(code);
			if (null != wxUser) {
				url = getRedirectUrl();
				MySecurityContextHandle.setItem("wchatId", wxUser.getOpenId());
				return SUCCESS;
			}
		}
		setPnParam();
		String redirectUri = CommonConfig.domainName + CommonConfig.contextPath + "/weixinauth/wxuserinfo.action";
		url = WxAuth.getAuthUrl(WxAuth.AUTH_TYPE_BASE, state, redirectUri);
		return SUCCESS;
	}

	private void setPnParam() {
		if (StringUtils.isEmpty(state)) {
//			String param = ServletActionContext.getRequest().getQueryString();
			state = UUID.randomUUID().toString().replaceAll("-", "");
			stateMap.put(state, param);
		}
	}

	private String getRedirectUrl() {
		String search = null;
		String pageName = null;
		String param = stateMap.remove(state);
		if (!StringUtils.isEmpty(param)) {
			JSONObject jo = JSONObject.fromObject(param);
			Set<String> keys = jo.keySet();
			for (String key : keys) {
				if ("pageName".equals(key)) {
					pageName = jo.getString(key);
					continue;
				}
				if (null == search) {
					search = "?";
				} else {
					search += "&";
				}
				search += key + "=" + jo.getString(key);
			}
		}
		if (StringUtils.isEmpty(pageName))
			pageName = "index.html";
		if (StringUtils.isEmpty(search))
			search = "";
		url = CommonConfig.domainName + CommonConfig.contextPath + pageName + search;
		return url;
	}
}
